-- resolve_equips
-- create by cheny3
-- 分解装备

return {
    -- 判断
    canTrigger = function(args)
        local layer = DungeonM.currentLayer();
        local step = args["step"] or 1;
        if layer % step ~= 0 then
            -- 没到指定层数
            return false;
        end
        return true;
    end,

    -- 触发
    trigger = function(args)
        local chanceRange = args["chance"];
        if chanceRange then
            if math.random(chanceRange[2]) > chanceRange[1] then
                -- 概率没触发
                return false;
            end
        end

        -- 获取玩家当前所有宝物
        local equips = EquipM.getUserTreasureList();

        -- 整理一下
        local splitLists = {};
        for classId, amount in pairs(equips) do
            -- 因为euqips包含装备的武器，所以这么处理
            if ItemM.getAmount(ME.user, classId) > 0 then
                table.insert(splitLists, classId);
            end
        end

        local resolveType = args["type"] or "one";
        local doTimes = args["count"];
        for i = 1, doTimes do
            if #splitLists <= 0 then
                -- 没装备了
                break;
            end

            -- 随机分解一件
            local randomNum = math.random(#splitLists);
            local classId = splitLists[randomNum];
            table.remove(splitLists, randomNum);
            local num = ItemM.getAmount(ME.user, classId);
            if resolveType == "one" then
                num = 1;
            end
            -- 分解装备
            DungeonActionM.go("split_equip", classId, num);
        end
        return true;
    end,
};
